var originalWindowWidth = 0;
var gForceOverflow = false;

function forceToolbarOverflow(){
	gForceOverflow = true;
	scribefireToolbarOverflow();
	gForceOverflow = false;
}

function scribefireToolbarOverflow() {
	var toolbar = document.getElementById('firstrowtoolbar');
	var overflowMenu = document.getElementById("scribefire-toolbar-more-menu");
	var menuDisplay = overflowMenu.style.display;
	
	overflowMenu.style.display = '';
	
	// Find how much space the toolbar should be allotted
	var windowWidth = document.getElementById("performancing-message-source").boxObject.width;
	
	// Find out how much space the toolbar is actually taking.
	var toolbarWidth = overflowMenu.boxObject.x + overflowMenu.boxObject.width;
	
	if (!gForceOverflow && ((windowWidth == 0) || (originalWindowWidth == windowWidth))){
		overflowMenu.style.display = menuDisplay;
		return false;
	}
	else{
		originalWindowWidth = windowWidth;
	}
	
	// Get the array number of the last child (skip the overflow menu)
	var lastChildN = toolbar.childNodes.length - 2;
	
	if (lastChildN > 0){
		// Get the "last" child
		var lastChild = toolbar.childNodes[lastChildN];
		
		var i = 0;
		
		while(toolbarWidth < (windowWidth - 25)){
			// While we have extra space in the toolbar, convert menu items to
			// toolbar buttons.
			var firstMenuItem = overflowMenu.firstChild.firstChild;
			
			if (firstMenuItem == null) {
				overflowMenu.style.display = 'none';
				return true;
			}
			
			++i;
			
			var button;
			
			if (firstMenuItem.getAttribute("oldTagName") == 'colorpicker') {
				firstMenuItem.setAttribute("disabled", "false");
				button = document.createElement("colorpicker");
				button.setAttribute("type", "button");
				button.type = "button";
				button.color = firstMenuItem.color;
				toolbar.insertBefore(button, overflowMenu);
			}
			else if(firstMenuItem.tagName == 'menuitem') {
				button = toolbar.ownerDocument.createElement('toolbarbutton');
				toolbar.insertBefore(button, overflowMenu);
				button.type = 'button';
			} else if(firstMenuItem.tagName == 'menu') {
				button = toolbar.ownerDocument.createElement('toolbarbutton');
				toolbar.insertBefore(button, overflowMenu);
				button.type = 'menu-button';
				button.appendChild(firstMenuItem.firstChild);
			} else if(firstMenuItem.tagName == 'menuseparator') {
				button = toolbar.ownerDocument.createElement('toolbarseparator');
				toolbar.insertBefore(button, overflowMenu);
				overflowMenu.firstChild.removeChild(firstMenuItem);
				lastChildN++;
				lastChild = button;
				continue;
			}
			
			button.setAttribute("id", firstMenuItem.getAttribute("id"));
			button.setAttribute("tooltiptext", firstMenuItem.getAttribute("label"));
			button.setAttribute("command", firstMenuItem.getAttribute("command"));
			button.setAttribute("oncommand", firstMenuItem.getAttribute("oncommand"));
			button.setAttribute("onchange",firstMenuItem.getAttribute("onchange"));
			button.setAttribute("tooltiptext", firstMenuItem.getAttribute("tooltiptext"));
			button.setAttribute("label", firstMenuItem.getAttribute("buttonlabel"));
			button.setAttribute("type", firstMenuItem.getAttribute("type"));
			button.setAttribute("disabled", firstMenuItem.getAttribute("disabled"));
			
			overflowMenu.firstChild.removeChild(firstMenuItem);

			lastChildN++;
			lastChild = button;
			toolbarWidth = overflowMenu.boxObject.x + overflowMenu.boxObject.width;
		}
	
		overflowMenu.style.display = '';
		
		var overflowMenuBox = overflowMenu.ownerDocument.getBoxObjectFor(overflowMenu);
		var spaceNeeded = overflowMenuBox.x + overflowMenuBox.width - windowWidth;
		
		while(spaceNeeded > 0) {
			var firstMenuItem = overflowMenu.firstChild.firstChild;
			var newMenuItem;
			
			if(lastChild.tagName == 'toolbarseparator') {
				newMenuItem = toolbar.ownerDocument.createElement('menuseparator');
				if(firstMenuItem != null) {
					overflowMenu.firstChild.insertBefore(newMenuItem, firstMenuItem);
				} else {
					overflowMenu.firstChild.appendChild(newMenuItem);
				}
				toolbar.removeChild(lastChild);
				lastChildN--;
				lastChild = toolbar.childNodes[lastChildN];
				continue;
			} else if(lastChild.tagName == 'toolbaritem') {
				lastChildN--;
				lastChild = toolbar.childNodes[lastChildN];
				continue;
			} else if(lastChild.type == 'menu') {
				newMenuItem = toolbar.ownerDocument.createElement('menu');
				if(firstMenuItem != null) {
					overflowMenu.firstChild.insertBefore(newMenuItem, firstMenuItem);
				} else {
					overflowMenu.firstChild.appendChild(newMenuItem);
				}
				newMenuItem.appendChild(lastChild.firstChild);
				toolbar.removeChild(lastChild);
			} else if(lastChild.type == 'menu-button') {
				newMenuItem = toolbar.ownerDocument.createElement('menu');
				if(firstMenuItem != null) {
					overflowMenu.firstChild.insertBefore(newMenuItem, firstMenuItem);
				} else {
					overflowMenu.firstChild.appendChild(newMenuItem);
				}
				newMenuItem.appendChild(lastChild.firstChild);
				toolbar.removeChild(lastChild);
			} else if(lastChild.type == 'button' || lastChild.type == null || lastChild.type == '') {
				newMenuItem = toolbar.ownerDocument.createElement('menuitem');
				
				if (lastChild.tagName == 'colorpicker') {
					lastChild.setAttribute("disabled", "true");
					newMenuItem.color = lastChild.color;
				}
				
				if(firstMenuItem != null) {
					overflowMenu.firstChild.insertBefore(newMenuItem, firstMenuItem);
				} else {
					overflowMenu.firstChild.appendChild(newMenuItem);
				}
				toolbar.removeChild(lastChild);
			}
	
			newMenuItem.setAttribute("id", lastChild.getAttribute("id"));
			newMenuItem.setAttribute("label", lastChild.getAttribute("tooltiptext"));
			newMenuItem.setAttribute("command", lastChild.getAttribute("command"));
			newMenuItem.setAttribute("oncommand", lastChild.getAttribute("oncommand"));
			newMenuItem.setAttribute("onchange",lastChild.getAttribute("onchange"));
			newMenuItem.setAttribute("tooltiptext", lastChild.getAttribute("tooltiptext"));
			newMenuItem.setAttribute("disabled", lastChild.getAttribute("disabled"));
			newMenuItem.setAttribute("buttonlabel", lastChild.getAttribute("label"));
			newMenuItem.setAttribute("type", lastChild.getAttribute("type"));
			newMenuItem.setAttribute("oldTagName", lastChild.tagName);
			
			newMenuItem.style.display = lastChild.style.display;
			
			lastChildN--;
			lastChild = toolbar.childNodes[lastChildN];
			
			spaceNeeded = overflowMenuBox.x + overflowMenuBox.width - windowWidth;
		}
	}
	
	return true;
}